﻿using RpcCacheClient.Interface;
using RpcCentral.Common;
using RpcCentral.DAL;
using RpcCentral.Model;
using RpcModel.Model;

namespace RpcCentral.Collect.Collect
{
    internal class RpcControlCollect : IRpcControlCollect
    {
        private IRpcControlListDAL _Controls;
        private ICacheController _Cache;
        private static string _CacheKey = "ControlServer";
        public RpcControlCollect(IRpcControlListDAL controls, ICacheController cache)
        {
            _Controls = controls;
            _Cache = cache;
        }

        public RpcControlServer[] GetControlServer()
        {
            if (this._Cache.TryGet(_CacheKey, out RpcControlServer[] servers))
            {
                return servers;
            }
            servers = this._Controls.GetControlServer().ConvertMap<RpcControl, RpcControlServer>();
            this._Cache.Set(_CacheKey, servers, new TimeSpan(0, 10, 0));
            return servers;
        }

        public void Refresh()
        {
            this._Cache.Remove("ControlServer");
        }
    }
}
